package com.ws.universal.tools.adapter;


import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import com.ws.universal.tools.utils.LogcatUtils;

import java.util.List;

/**
 * 相对于页面较多，且动态添加删除时 调用
 */
public class LiveViewPagerFragmentStatePagerAdapter extends FragmentStatePagerAdapter {

    public List<Fragment> mFragmentList;


    public LiveViewPagerFragmentStatePagerAdapter(@NonNull FragmentManager fm, int behavior) {
        super(fm, behavior);

    }


    public void setFragmentList(List<Fragment> mFragmentList) {

        this.mFragmentList = mFragmentList;
        notifyDataSetChanged();
    }

    public List<Fragment> getmFragmentList() {
        return mFragmentList;
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        // 新建一个Fragment来展示ViewPager item的内容，并传递参数
        Fragment fragment = mFragmentList.get(position);
        return fragment;
    }

    @Override
    public int getCount() {
        if (mFragmentList != null) {
            return mFragmentList.size();
        } else {
            return 0;
        }

    }

    //解決 Android NullPointerException FragmentHostCallback.getHandler()
    @Override
    public void finishUpdate(@NonNull ViewGroup container) {
        try {
            super.finishUpdate(container);
        } catch (NullPointerException nullPointerException) {
            LogcatUtils.e("", "Catch the NullPointerException in FragmentPagerAdapter.finishUpdate");
        }

    }
}
